import { response } from "express"
import { request } from "express"
import CODE from "../../config/data.config.js";
import DiaryService from "../../service/diaryService/diaryService.js"
import * as service from "../../service/service.js"


class DiaryController {
    /**
     * @param {request} req
     * @param {response} res
     */
    static async GetDiaryList(req, res, next) {
        try {
            let { userID, startLimit, endLimit, onlyuser } = req.query; // 参数都是字符串注意转换
            if (onlyuser == 'false') {
                onlyuser = false
            }
            const reslut = await DiaryService.GetDiaryList(userID, startLimit, endLimit, onlyuser)
            res.send({
                msg: '查询成功',
                code: 1,
                data: reslut
            }).end()
        } catch (error) {
            next({ error })
        }
    }

    /**
     * 获取全部的伴宠日记，或者未审核
     * @param {request} req 
     * @param {response} res 
     * @param {*} next 
     */
    static async GetAllDiaryList(req, res, next) {
        try {
            let { state } = req.query
            let result = await service.DiaryService.GetAllDiaryList(state)
            res.send({
                msg: '获取成功',
                code: CODE.CODE_OK,
                data: result
            })
        } catch (error) {
            next(error)
        }
    }

    /**
     * @param {request} req
     * @param {response} res
     * post请求
     */
    static async InsertDiary(req, res, next) {
        try {
            const { userID, petID, address, issueTime, introduction, introductionSrc } = req.body;
            let condition = { userID, petID, address, issueTime, introduction, introductionSrc }
            const status = await DiaryService.InsertDiary(condition)
            if (status) {
                res.send({
                    msg: '发布成功',
                    code: 1
                })
            } else next({ error: '发布失败' })
        } catch (error) {
            next(error)
        }
    }

    /**
     * 修改日记
     * @param {request} req
     * @param {response} res
     */
    static async EditDiary(req, res, next) {
        try {
            let { model, columns } = req.body
            let state = await service.DiaryService.EditDiary(model, columns)
            if (state) {
                res.send({
                    msg: '修改状态成功',
                    code: CODE.CODE_OK,
                    data: state
                })
            } else res.send({
                msg: '修改状态失败',
                code: CODE.CODE_FAIL,
                data: state
            })
        } catch (error) {
            next(error)
        }
    }
}


export default DiaryController;